﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project_1
{
    public partial class Form1 : Form
    {
        static Tire originalTire = new Tire();
        ReplacementTire upgradedTire = new ReplacementTire(originalTire);

        public Form1()
        {
            InitializeComponent();
            StartOver();
        }
        
        private void displayFactorySpecs()
        {
            double tireWidthIn = originalTire.CalculateWidth();
            factoryTireWidthInches.Text = tireWidthIn.ToString();

            double sideWallIn = originalTire.CalculateSidewall();
            factoryTireSidewallInches.Text = sideWallIn.ToString();

            double heightIn = originalTire.CalculateHeight();
            factoryTireHeightInches.Text = heightIn.ToString();

            double circumference = originalTire.CalculateCircumference();
            factoryTireCircumInches.Text = circumference.ToString();

            double revPerMile = originalTire.CalculateRevsPerMile(Convert.ToDouble(factoryTireSpeed.Value));
            factoryTireRevsPerMile.Text = revPerMile.ToString();
        }

        private void displayNewSpecs()
        {
            double tireWidthIn = upgradedTire.CalculateWidth();
            newTireWidthInches.Text = tireWidthIn.ToString();

            double sideWallIn = upgradedTire.CalculateSidewall();
            newTireSidewallInches.Text = sideWallIn.ToString();

            double heightIn = upgradedTire.CalculateHeight();
            newTireHeightInches.Text = heightIn.ToString();

            double circumference = upgradedTire.CalculateCircumference();
            newTireCircumInches.Text = circumference.ToString();

            double revPerMile = upgradedTire.CalculateRevsPerMile(Convert.ToDouble(factoryTireSpeed.Value));
            newTireRevsPerMile.Text = revPerMile.ToString();
        }

        private void displayNewSpeed()
        {
            double factorySpeed = Convert.ToDouble(factoryTireSpeed.Value);
            double factoryRevs = Convert.ToDouble(factoryTireRevsPerMile.Text);
            double newRevs = Convert.ToDouble(newTireRevsPerMile.Text);
            double newCircum = Convert.ToDouble(newTireCircumInches.Text);
            double newSpeed = upgradedTire.CalculateSpeed(factorySpeed, factoryRevs, newRevs, newCircum);
            newTireSpeed.Text = newSpeed.ToString();
            displaySuspensionLift();
        }
        
        private void clearFactoryTireFields()
        {
            factoryTireWidthInches.Text = "";
            factoryTireSidewallInches.Text = "";
            factoryTireHeightInches.Text = "";
            factoryTireCircumInches.Text = "";
            factoryTireRevsPerMile.Text = "";
        }

        private void clearNewTireFields()
        {
            newTireWidthInches.Text = "";
            newTireSidewallInches.Text = "";
            newTireHeightInches.Text = "";
            newTireCircumInches.Text = "";
            newTireRevsPerMile.Text = "";
            newTireSpeed.Text = "";
        }

        private void clearNewSpeedField()
        {
            newTireSpeed.Text = "";
        }

        private void factoryTireWidth_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (factoryTireWidth.Text != "")
                {
                    originalTire.MMWidth = Convert.ToInt16(factoryTireWidth.Text);
                    factoryCostLabel.Text = originalTire.CostIndicator();
                    factoryCostLabel.Visible = true;
                }

                if (factoryTireWidth.Text != "" && factoryTirePercent.Text != "" && factoryTireRim.Text != "")
                {
                    displayFactorySpecs();
                    numericUpDown1_ValueChanged(sender, e);
                }
                else
                {
                    clearFactoryTireFields();
                    clearNewSpeedField();
                }
            }
            catch
            {
                MessageBox.Show("Only numbers are allowed", "Input Error");
                factoryTireWidth.Text = "";
            }
        }

        private void factoryTirePercent_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (factoryTirePercent.Text != "")
                    originalTire.PercentWidth = Convert.ToInt16(factoryTirePercent.Text);

                if (factoryTireWidth.Text != "" && factoryTirePercent.Text != "" && factoryTireRim.Text != "")
                {
                    displayFactorySpecs();
                    numericUpDown1_ValueChanged(sender, e);
                }
                else
                {
                    clearFactoryTireFields();
                    clearNewSpeedField();
                }
            }
            catch
            {
                MessageBox.Show("Only numbers are allowed", "Input Error");
                factoryTirePercent.Text = "";
            }
        }

        private void factoryTireRim_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (factoryTireRim.Text != "")
                    originalTire.RimSize = Convert.ToInt16(factoryTireRim.Text);

                if (factoryTireWidth.Text != "" && factoryTirePercent.Text != "" && factoryTireRim.Text != "")
                {
                    displayFactorySpecs();
                    numericUpDown1_ValueChanged(sender, e);
                }
                else
                {
                    clearFactoryTireFields();
                    clearNewSpeedField();
                }
            }
            catch
            {
                MessageBox.Show("Only numbers are allowed", "Input Error");
                factoryTireRim.Text = "";
            }
        }

        private void newTireWidth_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (newTireWidth.Text != "")
                {
                    upgradedTire.MMWidth = Convert.ToInt16(newTireWidth.Text);
                    newCostLabel.Text = upgradedTire.CostIndicator();
                    newCostLabel.Visible = true;
                }

                if (newTireWidth.Text != "" && newTirePercent.Text != "" && newTireRim.Text != "")
                {
                    displayNewSpecs();
                        displayNewSpeed();
                }
                else
                    clearNewTireFields();
            }
            catch
            {
                MessageBox.Show("Only numbers are allowed", "Input Error");
                newTireWidth.Text = "";
            }
        }

        private void newTirePercent_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (newTirePercent.Text != "")
                    upgradedTire.PercentWidth = Convert.ToInt16(newTirePercent.Text);

                if (newTireWidth.Text != "" && newTirePercent.Text != "" && newTireRim.Text != "")
                    {
                        displayNewSpecs();
                            displayNewSpeed();
                    }
                    else
                        clearNewTireFields();
            }
            catch
            {
                MessageBox.Show("Only numbers are allowed", "Input Error");
                newTirePercent.Text = "";
            }
        }

        private void newTireRim_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (newTireRim.Text != "")
                    upgradedTire.RimSize = Convert.ToInt16(newTireRim.Text);

                if (newTireWidth.Text != "" && newTirePercent.Text != "" && newTireRim.Text != "")
                {
                    displayNewSpecs();
                        displayNewSpeed();
                }
                else
                    clearNewTireFields();
            }
            catch
            {
                MessageBox.Show("Only numbers are allowed", "Input Error");
                newTireRim.Text = "";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            originalTire.CheckCoolness();
            upgradedTire.CheckCoolness();

            string msg;

            if (originalTire.Coolness)
                msg = "Your factory tire is pretty cool, and ";
            else
                msg = "Your factory tire is uncool, and ";

            if (upgradedTire.Coolness)
                msg += "your new replacement tire is super cool.";
            else
                msg += "your new replacement tire is lame.";

            if (newTireSpeed.Text != "")
                if (upgradedTire.CheckSpeedDisplay(Convert.ToDouble(factoryTireSpeed.Value), Convert.ToDouble(newTireSpeed.Text)))
                    msg += " You should recalibrate the speedometer.";

            MessageBox.Show(msg, "Coolness Factor");
        }

        private void displaySuspensionLift()
        {
            if (factoryTireHeightInches.Text != "" && newTireCircumInches.Text != "")
            {
                bool lift = upgradedTire.CheckTireFit(Convert.ToDouble(factoryTireHeightInches.Text));

                if (lift)
                    suspensionLabel.Text = "Suspension Lift Required: Yes";
                else
                    suspensionLabel.Text = "Suspension Lift Required: No";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            StartOver();
        }

        private void StartOver()
        {
            clearFactoryTireFields();
            clearNewSpeedField();
            clearNewTireFields();
            factoryTireWidth.Text = "";
            factoryTirePercent.Text = "";
            factoryTireRim.Text = "";
            newTireWidth.Text = "";
            newTirePercent.Text = "";
            newTireRim.Text = "";
            factoryTireSpeed.Value = 70;
            suspensionLabel.Text = "Suspension Lift Required: N/A";
            factoryCostLabel.Visible = false;
            newCostLabel.Visible = false;
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            if (factoryTireWidth.Text != "" && factoryTirePercent.Text != "" && factoryTireRim.Text != "")
                displayFactorySpecs();

            if (newTireWidth.Text != "" && newTirePercent.Text != "" && newTireRim.Text != "")
            {
                displayNewSpecs();
                displayNewSpeed();
            }
            else
                clearNewSpeedField();
        }
    }
}
